package com.fc.test.service;

import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.PaperSheetMapper;
import com.fc.test.mapper.auto.TsysDatasMapper;
import com.fc.test.model.auto.*;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.QrCode.QRCode;
import com.fc.test.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 票据表 PaperSheetService
 *
 * @author 谢志恺_自动生成
 * @Title: PaperSheetService.java 
 * @Package com.fc.test.service 
 * @email 2251719256@qq.com
 * @date 2020-10-28 13:29:36  
 **/
@Service
public class PaperSheetService implements BaseService<PaperSheet, PaperSheetExample> {
    @Autowired
    private PaperSheetMapper paperSheetMapper;
    @Autowired
    private TsysDatasMapper tsysDatasMapper;
	@Autowired
	private PaperRecordsService paperRecordsService;


    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public PageInfo<PaperSheet> list(Tablepar tablepar, PaperSheet record) {
        PaperSheetExample testExample = new PaperSheetExample();
        //搜索
        if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
            testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
        } else {//大搜索
            testExample.createCriteria().andLikeQuery(record);
        }
        //表格排序
        if (StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
            testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) + " " + tablepar.getIsAsc());
        } else {
            testExample.setOrderByClause("id ASC");
        }
        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<PaperSheet> list = paperSheetMapper.selectByExample(testExample);
        PageInfo<PaperSheet> pageInfo = new PageInfo<PaperSheet>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {

        Integer[] integers = ConvertUtil.toIntArray(",", ids);
        List<Integer> stringB = Arrays.asList(integers);
        PaperSheetExample example = new PaperSheetExample();
        example.createCriteria().andIdIn(stringB);
        return paperSheetMapper.deleteByExample(example);


    }


    @Override
    public PaperSheet selectByPrimaryKey(String id) {

        Integer id1 = Integer.valueOf(id);
        return paperSheetMapper.selectByPrimaryKey(id1);

    }


    @Override
    public int updateByPrimaryKeySelective(PaperSheet record) {
        return paperSheetMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(PaperSheet record) {

        record.setId(null);
        record.setCreationTime(new Date());
        int i = paperSheetMapper.insertSelective(record);
        QRCode qrCode = new QRCode();
        String url = "C:/IMG/";
        String filePath = qrCode.generateTwoDimensionalCode(String.valueOf(record.getId()), url, String.valueOf(record.getId()), ".jpg");
        if (filePath.equals("false")) {
            return 0;
        }
        TsysDatas tsysDatas = new TsysDatas();
        tsysDatas.setId(String.valueOf(record.getId()));
        tsysDatas.setFilePath(filePath);
        tsysDatas.setFileSuffix(".jpg");
        tsysDatasMapper.insertSelective(tsysDatas);
        return record.getId();
    }

    @Override
    public int updateByExampleSelective(PaperSheet record, PaperSheetExample example) {

        return paperSheetMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(PaperSheet record, PaperSheetExample example) {

        return paperSheetMapper.updateByExample(record, example);
    }

    @Override
    public List<PaperSheet> selectByExample(PaperSheetExample example) {

        return paperSheetMapper.selectByExample(example);
    }


    @Override
    public long countByExample(PaperSheetExample example) {

        return paperSheetMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(PaperSheetExample example) {

        return paperSheetMapper.deleteByExample(example);
    }

    /**
     * 检查name
     *
     * @param paperSheet
     * @return
     */
    public int checkNameUnique(PaperSheet paperSheet) {
        PaperSheetExample example = new PaperSheetExample();
        example.createCriteria().andPohoneEqualTo(paperSheet.getPohone());
        List<PaperSheet> list = paperSheetMapper.selectByExample(example);
        return list.size();
    }

    /**
     * 根据主键查询单据和消费记录
     */
    public PaperSheetOrPaperRecord queryPaperSheetOrPaperRecord(String id) {
		PaperSheetOrPaperRecord paperSheetOrPaperRecord = new PaperSheetOrPaperRecord();
		List<PaperRecords> paperRecords = paperRecordsService.getPaperRecords(Integer.parseInt(id));
		if (!Objects.isNull(paperRecords)) {
			paperSheetOrPaperRecord.setPaperRecords(paperRecords);
		} else {
			paperSheetOrPaperRecord.setPaperRecords(null);
		}
		PaperSheet paperSheet = paperSheetMapper.selectByPrimaryKey(Integer.valueOf(id));
		paperSheetOrPaperRecord.setPaperSheet(paperSheet);
		return paperSheetOrPaperRecord;
    }
}
